package com.googlecode.messwertepi.model;

import com.googlecode.messwertepi.event.DataModelListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by eike on 24.06.14.
 */
public class DataModel
{
    public String input;
    public List<String> data;

    public List<DataModelListener> listeners = new ArrayList<DataModelListener>();

    public DataModel(String input, List<String> data)
    {
        this.input = input;
        this.data = data;
    }

    public void addDataModelListener(DataModelListener listener)
    {
        listeners.add(listener);
    }

    public void removeDataModelListener(DataModelListener listener)
    {
        listeners.remove(listener);
    }

    /**
     * Calls all DataModelListeners that are registered in this model
     * to indicate that the input string changed.
     */
    private void fireInputChanged()
    {
        for (DataModelListener listener : listeners) {
            listener.inputChanged(input);
        }
    }

    /**
     * Calls all DataModelListeners that are registered in this model
     * to indicate that the list of rows changed.
     */
    private void fireDataChanged()
    {
        for (DataModelListener listener : listeners) {
            listener.dataChanged(data);
        }
    }

    public String getInput()
    {
        return input;
    }

    public void setInput(String input)
    {
        this.input = input;
        fireInputChanged();
    }

    public List<String> getData()
    {
        return data;
    }

    public void setData(List<String> data)
    {
        this.data = data;
        fireDataChanged();
    }

    public void setData(String input, int index) {
        data.set(index, input);
        fireDataChanged();
    }

}
